package com.doupi.partner.op.controller.configure;

import com.doupi.partner.dal.model.AdministratorPermission;
import com.doupi.partner.dal.model.AdministratorRole;
import com.doupi.partner.dal.model.AdministratorRoleHasPermission;
import com.doupi.partner.service.AdministratorPermissionService;
import com.doupi.partner.service.AdministratorRoleService;
import com.doupi.partner.service.AdministratorRoleHasPermissionService;
import com.doupi.partner.service.dto.AdministratorRoleDto;
import com.doupi.partner.util.AjaxResponse;
import com.doupi.partner.util.DataTableData;
import com.doupi.partner.util.ObjectUtils;
import com.doupi.partner.util.Pagination;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("configure/administratorRole")
public class AdministratorRoleController {
	private final static String TEMPLATE_FOLDER = "configure/";

	private final AdministratorRoleService administratorRoleService;

	private final AdministratorRoleHasPermissionService administratorRoleHasPermissionService;

	private final AdministratorPermissionService administratorPermissionService;

	@Autowired
	public AdministratorRoleController(AdministratorRoleService administratorRoleService, AdministratorRoleHasPermissionService administratorRoleHasPermissionService,
			AdministratorPermissionService administratorPermissionService) {
		this.administratorRoleService = administratorRoleService;
		this.administratorRoleHasPermissionService = administratorRoleHasPermissionService;
		this.administratorPermissionService = administratorPermissionService;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index() {
		return TEMPLATE_FOLDER + "administratorRole/index";
	}

	@ResponseBody
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public DataTableData<AdministratorRole> list(Integer offset, Integer limit, AdministratorRole administratorRole) {
		Pagination<AdministratorRole, AdministratorRole> pagination = new Pagination<>(offset, limit, administratorRole);
		pagination = administratorRoleService.queryAdministratorRoles(pagination);
		List<AdministratorRole> lists = new ArrayList<>();
		for (AdministratorRole adRole : pagination.getRecords()) {
			AdministratorRoleDto administratorRoleDto = new AdministratorRoleDto();
			ObjectUtils.copyProperties(administratorRoleDto, adRole);
			List<AdministratorRoleHasPermission> temps = administratorRoleHasPermissionService.queryByAdministratorRoleId(adRole.getId());
			List<AdministratorPermission> administratorPermissions = new ArrayList<>();
			for (AdministratorRoleHasPermission administratorRoleToAdministratorPermission : temps) {
				administratorPermissions.add(administratorPermissionService.selectByPrimaryKey(administratorRoleToAdministratorPermission.getAdministratorPermissionId()));
			}
			administratorRoleDto.setAdministratorPermissions(administratorPermissions);
			lists.add(administratorRoleDto);
		}
		return new DataTableData<>(pagination.getTotalCount(), lists);
	}

	@Transactional
	@ResponseBody
	@RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE)
	public AjaxResponse delete(@PathVariable("id") Long id) {
		administratorRoleService.deleteByPrimaryKey(id);
		return AjaxResponse.defaultResponse();
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(ModelMap modelMap, @PathVariable(value = "id") Long id) {
		AdministratorRole administratorRole = administratorRoleService.selectByPrimaryKey(id);
		modelMap.put("administratorRole", administratorRole);
		if (null != administratorRole) {
			AdministratorRoleHasPermission arToAp = new AdministratorRoleHasPermission();
			arToAp.setAdministratorRoleId(administratorRole.getId());
			List<AdministratorRoleHasPermission> lists = administratorRoleHasPermissionService.queryAdministratorRoleHasPermissions(arToAp, new RowBounds(0, Integer.MAX_VALUE));
			modelMap.put("administratorRoleHasPermissions", lists);
		}
		return TEMPLATE_FOLDER + "administratorRole/edit";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(AdministratorRoleDto administratorRoleDto) {
		administratorRoleService.saveAdministratorRole(administratorRoleDto);
		return "redirect:index";
	}
}
